package com.oauth.linkedin.server.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.oauth.logging.RapwavesLogger;
import com.oauth.logging.RapwavesLoggerFactory;
import com.oauth.service.global.ApplicationGlobalServiceAdapter;
import com.oauth.service.global.beans.Birthday;
import com.oauth.service.global.beans.OAuthAccessToken;
import com.oauth.service.global.beans.OAuthRequestToken;
import com.rapwaves.feeds.Feeds;

@Service
public class LinkedinGlobalService extends ApplicationGlobalServiceAdapter {

	@Autowired
	private LinkedinService mLinkedinService;

	private RapwavesLogger logger = RapwavesLoggerFactory.getLogger(this.getClass());

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.oauth.service.application.global.ApplicationGlobalServiceAdapter#
	 * updateStatus(java.lang.Long, java.lang.String)
	 */
	@Override
	public boolean updateStatus(Long pMemberId, String pStatusText) {
		try {
			mLinkedinService.updateStatus(pStatusText, pMemberId);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("Error in updating linkedin status", e);
			return false;
		}
		return true;
	}

	public Feeds getFeeds(Long pMemberId) throws Exception{
		try {
			return mLinkedinService.getFeeds(pMemberId);
		} catch (Exception e) {
			logger.error("Error in fetching linkedin feeds", e);
			throw e;
		}
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.oauth.service.application.global.ApplicationGlobalServiceAdapter#
	 * getUpcomingBirthdays(java.lang.Long, int)
	 */
	@Override
	public List<Birthday> getUpcomingBirthdays(Long pMemberId, int pBirthdayInterval) {
		List<Birthday> upcomingBirthdays = new ArrayList<Birthday>(1);
		try {
			upcomingBirthdays = mLinkedinService.getUpcomingBirthdays(pMemberId, pBirthdayInterval);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("Error in getting linkedin birthdays", e);
		}
		return upcomingBirthdays;
	}

	/**
	 * @param pLinkedinService
	 *            the linkedinService to set
	 */
	public void setLinkedinService(LinkedinService pLinkedinService) {
		mLinkedinService = pLinkedinService;
	}

	/**
	 * @param pRequestToken
	 * @param pVerifier
	 * @return
	 * @see com.oauth.linkedin.server.service.LinkedinService#getAccessTokenFromRequestToken(com.oauth.service.global.beans.OAuthRequestToken,
	 *      java.lang.String)
	 */
	public OAuthAccessToken getAccessToken(OAuthRequestToken pRequestToken) {
		try {
			return mLinkedinService.getAccessTokenFromRequestToken(pRequestToken);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @return
	 * @see com.oauth.linkedin.server.service.LinkedinService#getRequestToken()
	 */
	public OAuthRequestToken getRequestToken() {
		try {
			return mLinkedinService.getRequestToken();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
